package com.sunny.ihealth.ui.fragment;


import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.sunny.ihealth.BaseFragment;
import com.sunny.ihealth.R;
import com.sunny.ihealth.data.BmobManager;
import com.sunny.ihealth.entity.bmob.State;
import com.sunny.ihealth.entity.bmob.UserRelation;
import com.sunny.ihealth.ui.activity.InviteYourFriendActivity;
import com.sunny.ihealth.ui.activity.ProfileActivity;
import com.sunny.ihealth.ui.adapter.FriendAndFansAdapter;
import com.sunny.ihealth.ui.adapter.OnViewClickListener;
import com.sunny.ihealth.utils.NetworkUtils;

import java.util.ArrayList;
import java.util.List;


public class FriendFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, OnViewClickListener {

    private static final String TAG = "TAG::FriendFragment";

    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private FriendAndFansAdapter adapter;

    private int type;
    private List<UserRelation> data;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle == null) {
            Toast.makeText(getContext(), "bundle is null", Toast.LENGTH_SHORT).show();
            Log.e(TAG, "bundle is null");
            return;
        }

        type = bundle.getInt("type", -1);
        if (type != FriendAndFansAdapter.TYPE_FRIEND && type != FriendAndFansAdapter.TYPE_FANS) {
            Toast.makeText(getContext(), "bundle type error", Toast.LENGTH_SHORT).show();
            Log.e(TAG, "bundle is error");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_friend, container, false);
        initViews(view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        swipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
                initData();
            }
        }, 1000);
    }

    @Override
    public void initViews(View v) {
        swipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.refresh_friend);
        swipeRefreshLayout.setOnRefreshListener(this);
        adapter = new FriendAndFansAdapter(getContext(), data = new ArrayList<>(), type);
        adapter.setListener(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView = (RecyclerView) v.findViewById(R.id.recyclerView_friend);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void initData() {
        getBmob().getFriends(type == FriendAndFansAdapter.TYPE_FRIEND, new BmobManager.Action1<List<UserRelation>>() {
            @Override
            public void onCompleted(List<UserRelation> list) {
                if (list != null) {
                    if (list.size() == 0) {
                        Toast.makeText(getContext(), "啊偶，什么都没加载出来~", Toast.LENGTH_SHORT).show();
                    } else {
                        data.clear();
                        data.addAll(list);
                        adapter.notifyDataSetChanged();
                    }
                }
                swipeRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }, 500);
            }
        });
    }

    @Override
    public void onRefresh() {
        initData();
    }

    @Override
    public void onViewClick(int viewID, final int position) {
        if (viewID == R.id.ll_itemFriend) {
            if (!NetworkUtils.isNetworkAvailable(getContext(), true)) {
                return;
            }
            //Intent intent = new Intent(getContext(), InviteYourFriendActivity.class);
            Intent intent = new Intent(getContext(), ProfileActivity.class);
            if (type == FriendAndFansAdapter.TYPE_FRIEND) {
                if (data.get(position).getBoth()) {
                    data.get(position).getFriend().setIsFriend(State.STATE_BOTH_FRIEND);
                } else {
                    data.get(position).getFriend().setIsFriend(State.STATE_ADD);
                }
                intent.putExtra("data", data.get(position).getFriend());
            } else {
                if (data.get(position).getBoth()) {
                    data.get(position).getUser().setIsFriend(State.STATE_BOTH_FRIEND);
                }
                intent.putExtra("data", data.get(position).getUser());
            }
            startActivity(intent);
        } else if (viewID == R.id.btn_itemFriend_more) {
            new AlertDialog.Builder(getContext())
                    .setTitle("操作")
                    .setItems(new String[]{"邀请TA跑步"}, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(getContext(), InviteYourFriendActivity.class);
                            if (type == FriendAndFansAdapter.TYPE_FRIEND) {
                                intent.putExtra("data", data.get(position).getFriend());
                            } else {
                                intent.putExtra("data", data.get(position).getUser());
                            }
                            startActivity(intent);
                        }
                    })
                    .create()
                    .show();
        }
    }
}
